package homework.module3;

import java.util.*;

public class Doudizhu {

    static List<Card> cardList = new ArrayList<>();
    static Map<Integer, String> cardMapper = new HashMap<>();  // 记录牌和数字的对应

    static{
        for (int i=3;i<18;i++){
            cardMapper.put(i,String.valueOf(i));
            if (i==11){
                cardMapper.put(i,"J");
            }else if(i==12){
                cardMapper.put(i,"Q");
            }else if(i==13){
                cardMapper.put(i,"K");
            }else if(i==14){
                cardMapper.put(i,"A");
            }else if(i==15){
                cardMapper.put(i,"2");
            }else if(i==16){
                cardMapper.put(i,"小王");
            }else if(i==17){
                cardMapper.put(i,"大王");
            }
        }
    }

    public void initCard(){

        for (int i=3;i<18;i++){
            if (i<16){
                for (int count=0;count<4;count++){
                    Card card = new Card(i);
                    card.setCardNumber(cardMapper.get(i));
                    cardList.add(card);
                }
            }else{  // 大小王各放一张，其余放4张
                Card card = new Card(i);
                cardList.add(card);
            }
        }

    }

    public static void printPlayerCard(Player player){
        List<Card> cardList = player.getCardList();
        List<Integer> printList = new ArrayList<>();

        String playerName = player.getName();
        String cardStr = "";

//        String cardNumber = card.getCardNumber();
//        cardStr += cardNumber + "\t";
        for (Card card: cardList){
            int number = card.getNumber();
            printList.add(number);
        }

        Collections.sort(printList);
        Collections.reverse(printList);
        for (int number: printList){
            String cardNumber = cardMapper.get(number);  // 获取扑克的图案
            cardStr += cardNumber + "   ";
        }

        System.out.println("玩家的名称：" + playerName + ",手牌数：" + cardList.size()  +",手牌是：" + cardStr);
    }


    public static void main(String[] args) {

        // 1. 准备54张扑克牌
        Doudizhu doudizhu = new Doudizhu();
        doudizhu.initCard();  // 初始化54张扑克牌

        Collections.shuffle(cardList);  // 洗牌

        // 2.由三个玩家交替摸牌....
        Player player1 = new Player("药水哥");
        Player player2 = new Player("武僧一龙");
        Player player3 = new Player("向左");

        int index = 53;
        while(index>2){

            if (index==1){
                System.out.println("---");
            }

            Card card = cardList.remove(index);

            int playerTurn = index%3;
            switch (playerTurn){
                case 0:
                    List<Card> cardList1 = player1.getCardList();
                    cardList1.add(card);
                    break;
                case 1:
                    List<Card> cardList2 = player2.getCardList();
                    cardList2.add(card);
                    break;
                case 2:
                    List<Card> cardList3 = player3.getCardList();
                    cardList3.add(card);
                    break;
            }
            index--;
        }

        // 最后的3张牌设为底牌
        for (Card card: cardList){
            card.setDipai(true);
        }


        // 3.查看玩家手牌和底牌, 按照大小打印
        printPlayerCard(player1);
        printPlayerCard(player2);
        printPlayerCard(player3);

        String dipai = "";
        for (Card card: cardList){
            String cardNumber = card.getCardNumber();
            dipai += cardNumber + " ";
        }
        System.out.println("底牌是：" + dipai);

    }


}
class Player{
    public Player(String name){
        this.name = name;
    }

    public List<Card> getCardList() {
        return cardList;
    }

    public void setCardList(List<Card> cardList) {
        this.cardList = cardList;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    List<Card> cardList = new ArrayList<>();  // 玩家持有扑克牌
    String name;
}


class Card{


    public boolean isDipai() {
        return isDipai;
    }

    public void setDipai(boolean dipai) {
        isDipai = dipai;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    int number;

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    String cardNumber;   // 牌的图案，包括j、q、k
    boolean isDipai=false;

    public Card(int number){
        this.number = number;
//            this.isDipai = isDipai;
    }

}